<?php

/**
 * 品酒会管理
 * $Author: sam iokdo@139.com
 * 2011-1-15 16:00
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
include_once(ROOT_PATH . 'includes/cls_image.php');
$image = new cls_image($_CFG['bgcolor']);

$exc = new exchange($ecs->table("tasting"), $db, 'tasting_id', 'title');

$link = array();
/*------------------------------------------------------ */
//-- 品酒会列表
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'list')
{
    assign_query_info();
    $smarty->assign('ur_here',     $_LANG['tasting_list']);
    $smarty->assign('action_link', array('text' => $_LANG['tasting_add'], 'href' => 'tasting.php?act=add'));
    $smarty->assign('full_page',   1);

    $tasting_list = tasting_list();

    $smarty->assign('tasting_list',    $tasting_list['tasting_list']);
    $smarty->assign('filter',       $tasting_list['filter']);
    $smarty->assign('record_count', $tasting_list['record_count']);
    $smarty->assign('page_count',   $tasting_list['page_count']);

    $smarty->display('tasting_list.htm');
}
/*------------------------------------------------------ */
//-- ajax列表
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'query')
{
    $tasting_list = tasting_list();
    $smarty->assign('tasting_list',    $tasting_list['tasting_list']);
    $smarty->assign('filter',       $tasting_list['filter']);
    $smarty->assign('record_count', $tasting_list['record_count']);
    $smarty->assign('page_count',   $tasting_list['page_count']);

    $sort_flag  = sort_flag($tasting_list['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);

    make_json_result($smarty->fetch('tasting_list.htm'), '', array('filter' => $tasting_list['filter'], 'page_count' => $tasting_list['page_count']));
}
/*------------------------------------------------------ */
//-- 删除贺卡
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'remove')
{
    /* 检查权限 */
    check_authz_json('tasting_manage');

    $tasting_id = empty($_REQUEST['id']) ? 0 : intval($_REQUEST['id']);

    $name = $exc->get_name($tasting_id);

    $img = $exc->get_name($tasting_id, 'img');

    if ($exc->drop($tasting_id))
    {
        /* 删除图片 */
        if (!empty($img))
        {
             @unlink('../' . DATA_DIR . '/tastingimg/'.$img);
        }
        admin_log(addslashes($name),'remove','tasting');

        $url = 'tasting.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);

        ecs_header("Location: $url\n");
        exit;
    }
    else
    {
        make_json_error($db->error());
    }
}
/*------------------------------------------------------ */
//-- 添加新包装
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'add')
{
    /* 权限判断 */
    admin_priv('tasting_manage');

    $smarty->assign('tasting',     $tasting);
    $smarty->assign('ur_here',     $_LANG['tasting_add']);
    $smarty->assign('action_link', array('text' => $_LANG['tasting_list'], 'href' => 'tasting.php?act=list'));
    $smarty->assign('form_action', 'insert');

    assign_query_info();
    $smarty->display('tasting_info.htm');

}
elseif ($_REQUEST['act'] == 'insert')
{
    /* 权限判断 */
    admin_priv('tasting_manage');

     /*处理图片*/
    $img_name = basename($image->upload_image($_FILES['img'],"tastingimg"));

    /*插入数据*/
    $sql = "INSERT INTO ".$ecs->table('tasting')."(title, link, content, img)
            VALUES ('$_POST[title]', '$_POST[link]', '$_POST[content]', '$img_name')";
    $db->query($sql);

    admin_log($_POST['title'],'add','tasting');

    /*添加链接*/
    $link[0]['text'] = $_LANG['continue_add'];
    $link[0]['href'] = 'tasting.php?act=add';

    $link[1]['text'] = $_LANG['back_list'];
    $link[1]['href'] = 'tasting.php?act=list';

    sys_msg($_POST['title'].$_LANG['tastingadd_succeed'],0, $link);
}

/*------------------------------------------------------ */
//-- 编辑包装
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'edit')
{
    /* 权限判断 */
    admin_priv('tasting_manage');

    $sql = "SELECT tasting_id, title, link, content, img FROM " .$ecs->table('tasting'). " WHERE tasting_id='$_REQUEST[id]'";
    $tasting = $db->GetRow($sql);

    $smarty->assign('ur_here',     $_LANG['tasting_edit']);
    $smarty->assign('action_link', array('text' => $_LANG['tasting_list'], 'href' => 'tasting.php?act=list&' . list_link_postfix()));
    $smarty->assign('tasting',        $tasting);
    $smarty->assign('form_action', 'update');

    assign_query_info();
    $smarty->display('tasting_info.htm');
}
elseif ($_REQUEST['act'] == 'update')
{
    /* 权限判断 */
    admin_priv('tasting_manage');

    $param = "title = '$_POST[title]', link = '$_POST[link]', content = '$_POST[content]'";
    /* 处理图片 */
    $img_name = basename($image->upload_image($_FILES['img'],"tastingimg", $_POST['old_img']));
    if ($img_name)
    {
        $param .= "  ,img ='$img_name' ";
    }

    if ($exc->edit($param,  $_POST['id']))
    {
        admin_log($_POST['title'], 'edit', 'tasting');

        $link[0]['text'] = $_LANG['back_list'];
        $link[0]['href'] = 'tasting.php?act=list&' . list_link_postfix();

        $note = sprintf($_LANG['tastingedit_succeed'], $_POST['title']);
        sys_msg($note, 0, $link);
    }
    else
    {
        die($db->error());
    }
}

/* 删除卡片图片 */
elseif ($_REQUEST['act'] == 'drop_tasting_img')
{
    /* 权限判断 */
    admin_priv('tasting_manage');
    $tasting_id = isset($_GET['id']) ? intval($_GET['id']) : 0;

    /* 取得logo名称 */
    $sql = "SELECT img FROM " .$ecs->table('tasting'). " WHERE tasting_id = '$tasting_id'";
    $img_name = $db->getOne($sql);

    if (!empty($img_name))
    {
        @unlink(ROOT_PATH . DATA_DIR . '/tastingimg/' .$img_name);
        $sql = "UPDATE " .$ecs->table('tasting'). " SET img = '' WHERE tasting_id = '$tasting_id'";
        $db->query($sql);
    }
    $link= array(array('text' => $_LANG['tasting_edit_lnk'], 'href'=>'tasting.php?act=edit&id=' .$tasting_id), array('text' => $_LANG['tasting_list_lnk'], 'href'=>'brand.php?act=list'));
    sys_msg($_LANG['drop_tasting_img_success'], 0, $link);
}
/*------------------------------------------------------ */
//-- ajax编辑卡片名字
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'edit_tasting_name')
{
    check_authz_json('tasting_manage');
    $tasting_id = empty($_REQUEST['id']) ? 0 : intval($_REQUEST['id']);
    $tasting_name = empty($_REQUEST['val']) ? '' : json_str_iconv(trim($_REQUEST['val']));

    if (!$exc->is_only('tasting_name', $tasting_name, $tasting_id))
    {
        make_json_error(sprintf($_LANG['tastingname_exist'], $tasting_name));
    }
    $old_tasting_name = $exc->get_name($tasting_id);
    if ($exc->edit("tasting_name='$tasting_name'", $tasting_id))
    {
        admin_log(addslashes($old_tasting_name), 'edit', 'tasting');
        make_json_result(stripcslashes($tasting_name));
    }
    else
    {
        make_json_error($db->error());
    }
}
/*------------------------------------------------------ */
//-- ajax编辑卡片费用
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'edit_tasting_fee')
{
    check_authz_json('tasting_manage');
    $tasting_id = empty($_REQUEST['id']) ? 0 : intval($_REQUEST['id']);
    $tasting_fee = empty($_REQUEST['val']) ? 0.00 : floatval($_REQUEST['val']);

    $tasting_name = $exc->get_name($tasting_id);
    if ($exc->edit("tasting_fee ='$tasting_fee'", $tasting_id))
    {
        admin_log(addslashes($tasting_name), 'edit', 'tasting');
        make_json_result($tasting_fee);
    }
    else
    {
        make_json_error($db->error());
    }
}
/*------------------------------------------------------ */
//-- ajax编辑免费额度
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'edit_free_money')
{
    check_authz_json('tasting_manage');
    $tasting_id = empty($_REQUEST['id']) ? 0 : intval($_REQUEST['id']);
    $free_money = empty($_REQUEST['val']) ? 0.00 : floatval($_REQUEST['val']);

    $tasting_name = $exc->get_name($tasting_id);
    if ($exc->edit("free_money ='$free_money'", $tasting_id))
    {
        admin_log(addslashes($tasting_name), 'edit', 'tasting');
        make_json_result($free_money);
    }
    else
    {
        make_json_error($db->error());
    }
}

function tasting_list()
{
    $result = get_filter();
    if ($result === false)
    {
        $filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'tasting_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);

        /* 分页大小 */
        $sql = "SELECT count(*) FROM " .$GLOBALS['ecs']->table('tasting');
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);

        $filter = page_and_size($filter);

        /* 查询 */
        $sql = "SELECT tasting_id, title, img, link, content".
               " FROM ".$GLOBALS['ecs']->table('tasting').
               " ORDER by " . $filter['sort_by'] . ' ' . $filter['sort_order'] .
               " LIMIT " . $filter['start'] . ',' . $filter['page_size'];

        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }

    $tasting_list = $GLOBALS['db']->getAll($sql);

    $arr = array('tasting_list' => $tasting_list, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);

    return $arr;
}

?>